<?php

namespace App\Admin\Controllers\Share;

use App\model\broker\BrokerRewardRecord;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use App\User;
use App\model\broker\BrokerCustomer;

class BrokerRewardRecordController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('推广奖励记录')
            ->description('推广奖励记录')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('推广奖励记录')
            ->description('推广奖励记录')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    /*public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }*/

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    /*public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }*/

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new BrokerRewardRecord);

        $grid->id('Id');
        $grid->reward('奖励金额');
        $grid->reward_type('奖励类型')->display(function ($value) {
            return BrokerRewardRecord::$reward_type_map[$value]?? '';
        });
        $grid->to_user_id('直推用户')->display(function ($value) {
            return User::find($value)->name ?? '';
        });
        $grid->from_user_id('推广用户')->display(function ($value) {
            return User::find($value)->name ?? '';
        });
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');
        $grid->disableCreateButton();
        $grid->disableActions();
        $grid->filter(function (Grid\Filter $filter) {
            // 去掉默认的id过滤器
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->equal('reward_type', '进度状态')->select(BrokerRewardRecord::$reward_type_map);
                $filter->equal('to_user_id', '直推用户')->mac(User::getLikeUser(\Request::get('user_id')??''));
            });
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(BrokerRewardRecord::findOrFail($id));

        $show->id('Id');
        $show->reward('奖励金额');
        $show->reward_type('奖励类型');
        $show->to_user_id('直推用户');
        $show->from_user_id('推广用户');
        $show->created_at('创建时间');
        $show->updated_at('更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    /*protected function form()
    {
        $form = new Form(new BrokerRewardRecord);

        $form->decimal('reward', 'Reward');
        $form->text('reward_type', 'Reward type');
        $form->number('to_user_id', 'To user id');
        $form->number('from_user_id', 'From user id');
        return $form;
    }*/
}
